#include "widget.h"
#include "ui_widget.h"

#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::on_pushButtonCharJudge_clicked()
{
    QString str = ui->lineEditInputChar->text();
    if ( str.isEmpty() ) {
        return;
    }

    QChar ch = str[0]; /* 只取第一个字符 */

    char16_t uniCode = ch.unicode(); /* 获取QChar字符的UTF-16编码值 */
    QString chStr(ch);
    QString info = chStr + QString::asprintf("\t, Unicode编码 = 0x%X\n", uniCode);
    ui->plainTextEditOutputText->appendPlainText(info);

    ui->checkBoxIsDigit->setChecked(ch.isDigit()); /* 是否为数字 0~9 */
    ui->checkBoxIsLetter->setChecked(ch.isLetter()); /* 是否为字母 */
    ui->checkBoxIsLetterOrNumber->setChecked(ch.isLetterOrNumber()); /* 是否为字母或数字 */
    ui->checkBoxIsUpper->setChecked(ch.isUpper()); /* 是否为大写 */
    ui->checkBoxIsLower->setChecked(ch.isLower()); /* 是否为小写 */
    ui->checkBoxIsMark->setChecked(ch.isMark()); /* 是否为标记 */
    ui->checkBoxIsSpace->setChecked(ch.isSpace()); /* 是否为空格 */
    ui->checkBoxIsSymbol->setChecked(ch.isSymbol()); /* 是否为符号 */
    ui->checkBoxIsPutct->setChecked(ch.isPunct()); /* 是否为标点符号 */
}

void Widget::on_pushButtonGetChar_clicked()
{
    QString str = ui->lineEditInputText->text();
    if ( str.isEmpty() ) {
        return;
    }

    ui->plainTextEditOutputText->clear();
    for (int i = 0; i < str.size(); ++i) {
        QChar ch = str.at(i);
        char16_t uniCode = ch.unicode();
        QString chStr(ch);
        QString info = chStr + QString::asprintf("\t, Unicode编码 = 0x%X\n", uniCode);
        ui->plainTextEditOutputText->appendPlainText(info);
    }
}

void Widget::on_pushButtonClearOut_clicked()
{
    ui->plainTextEditOutputText->clear();
}

void Widget::on_pushButtonToLatin1_clicked()
{
    QString str = "Dimple";
    ui->plainTextEditOutputText->clear();
    ui->plainTextEditOutputText->appendPlainText(str);

    QChar chP = QChar::fromLatin1('P');
    str[0] = chP;
    ui->plainTextEditOutputText->appendHtml("\n" + str);
}

void Widget::on_pushButtonToUtf16_clicked()
{
    QString str = "Hello,宁波";
    ui->plainTextEditOutputText->clear();
    ui->plainTextEditOutputText->appendPlainText(str);
    str[6] = QChar(0x9752);
    str[7] = QChar(0x5C9B);
    ui->plainTextEditOutputText->appendPlainText("\n" + str);
}

void Widget::on_pushButtonCmpRep_clicked()
{
    QString str = "他们来自于河南或河北";
    ui->plainTextEditOutputText->clear();
    ui->plainTextEditOutputText->appendPlainText(str);

    QString huStr = "河to湖";
    QChar he = QChar(huStr[0].unicode());
    QChar hu = QChar(huStr[3].unicode());

    for (int i = 0; i < str.size(); ++i) {
        if (str.at(i) == he) {
            str[i] = hu;
        }
    }
    ui->plainTextEditOutputText->appendPlainText("\n" + str);
}

